import { PageLoading } from '@ant-design/pro-layout';
import { history, Link } from 'umi';
import RightContent from '@/components/RightContent';
import Footer from '@/components/Footer';
import { currentUser as queryCurrentUser } from './services/api';
import { BookOutlined, LinkOutlined } from '@ant-design/icons';
import RunTimeLayoutConfig from 'umi/';
const isDev = process.env.NODE_ENV === 'development';
const loginPath = '/user/login';
/** 获取用户信息比较慢的时候会展示一个 loading */

export const initialStateConfig = {
  loading: <PageLoading />,
};
/**
 * @see  https://umijs.org/zh-CN/plugins/plugin-initial-state
 * */

export async function getInitialState() {
  const fetchUserInfo = async () => {
    try {
      const msg = await queryCurrentUser();
      console.log('msg', msg);
      return msg.data;
    } catch (error) {
      history.push(loginPath);
    }

    return undefined;
  }; // 如果是登录页面，不执行

  if (history.location.pathname !== loginPath) {
    const currentUser = await fetchUserInfo();
    return {
      fetchUserInfo,
      currentUser,
      settings: {},
    };
  }

  return {
    fetchUserInfo,
    settings: {},
  };
} // ProLayout 支持的api https://procomponents.ant.design/components/layout

// 从initialState 加载 菜单  可控制权限
// 从 initialState 加载layout 可控制权限
export const layout = ({ initialState }) => {
  console.log('layout', initialState);
  return {
    // 加载菜单
    menu: {
      // 每当 initialState?.currentUser?.userid 发生修改时重新执行 request
      params: initialState,
      request: async (params, defaultMenuData) => {
        return initialState.currentUser;
      },
    },
    rightContentRender: () => <RightContent />,
    disableContentMargin: false,
    waterMarkProps: {
      content: initialState?.currentUser?.name,
    },
    footerRender: () => <Footer />,
    onPageChange: () => {
      const { location } = history; // 如果没有登录，重定向到 login

      if (!initialState?.currentUser && location.pathname !== loginPath) {
        history.push(loginPath);
      }
    },
    menuHeaderRender: undefined,
    // 自定义 403 页面
    unAccessible: <div>unAccessible</div>,
    ...initialState?.settings,
  };
};

const route = [
  {
    path: '/user',
    layout: false,
    routes: [
      {
        path: '/user',
        routes: [
          {
            name: 'login',
            path: '/user/login',
            component: './user/Login',
          },
        ],
      },
      {
        component: './404',
      },
    ],
  },
  {
    component: './404',
  },
  // {
  //   path: '/',
  //   redirect: '/welcome',
  // },
  // {
  //   path: '/welcome',
  //   name: 'welcome',
  //   icon: 'smile',
  //   component: './Welcome',
  // },
  // {
  //   path: '/auth',
  //   name: '权限管理',
  //   icon: 'audit',
  //   routes: [
  //     {
  //       name: '管理员管理',
  //       path: '/auth/admin-management',
  //       component: './auth/adminManagement',
  //     },
  //     {
  //       name: '角色组',
  //       path: '/auth/role-group',
  //       component: './auth/roleGroup',
  //     },
  //     {
  //       component: './404',
  //     },
  //   ],
  // },
  // {
  //   path: '/system',
  //   name: '系统配置',
  //   icon: 'audit',
  //   routes: [
  //     {
  //       name: '接口规则',
  //       path: '/system/interface-rules',
  //       component: './system/interfaceRules',
  //     },
  //     {
  //       name: '菜单规则',
  //       path: '/system/menu-rules',
  //       component: './system/menuRules',
  //     },
  //     {
  //       component: './404',
  //     },
  //   ],
  // },
];
